Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization and deserialization of composed types in TS #5461

Merged
merged 10 commits into from
Nov 12, 2024

Conversation

rkodev
Copy link
Contributor

@rkodev rkodev commented Sep 25, 2024

Fix serialization and deserialization of composed types in TS
fixes #5353

Changes made to fix the serializer will include writing an undefined value for the string and and the collection of string

export function serializeSuccess(writer: SerializationWriter, success: Partial<string[] | string> | undefined | null = {}) : void {
    if(success){
        switch (true) {
            case typeof success === "string":
                writer.writeStringValue(undefined, success as string);
            break;
            case Array.isArray(success) && (success).every(item => typeof item === 'string') :
                writer.writeCollectionOfObjectValues<string>(undefined, success as string []);
            break;
        }
    }
}

changes made to fix thd deserializer. since undefined is not an acceptable key in a record, the serializers will have to invoke the method with an empty key i.e "" to as the assign the value of success to the node

export function deserializeIntoSuccess(success: Partial<Parsable | string[] | string> | undefined = {}) : Record<string, (node: ParseNode) => void> {
    return {
        "" : n => { success = n.getStringValue() ?? n.getCollectionOfPrimitiveValues<string>(); },
    }
}

@rkodev rkodev force-pushed the fix/ts-composed-types branch 4 times, most recently from 8b7c090 to 070f0ac Compare October 7, 2024 12:02
@rkodev
Copy link
Contributor Author

rkodev commented Oct 7, 2024

Sample builder

/* tslint:disable */
/* eslint-disable */
// Generated by Microsoft Kiota
// @ts-ignore
import { type BaseRequestBuilder, type Parsable, type ParsableFactory, type ParseNode, type RequestConfiguration, type RequestInformation, type RequestsMetadata, type SerializationWriter } from '@microsoft/kiota-abstractions';

/**
 * Creates a new instance of the appropriate class based on discriminator value
 * @param parseNode The parse node to use to read the discriminator value and create the object
 * @returns {string[] | string}
 */
// @ts-ignore
export function createSuccessFromDiscriminatorValue(parseNode: ParseNode | undefined) : ((instance?: Parsable) => Record<string, (node: ParseNode) => void>) {
    return deserializeIntoSuccess;
}
/**
 * The deserialization information for the current model
 * @returns {Record<string, (node: ParseNode) => void>}
 */
// @ts-ignore
export function deserializeIntoSuccess(success: Partial<Parsable | string[] | string> | undefined = {}) : Record<string, (node: ParseNode) => void> {
    return {
        "" : n => { success = n.getCollectionOfPrimitiveValues<string>() ?? n.getStringValue()},
    }
}
/**
 * Builds and executes requests for operations under /example1
 */
export interface Example1RequestBuilder extends BaseRequestBuilder<Example1RequestBuilder> {
    /**
     * Test generating error with message property.
     * @param requestConfiguration Configuration for the request such as headers, query parameters, and middleware options.
     * @returns {Promise<string[] | string>}
     */
     post(requestConfiguration?: RequestConfiguration<object> | undefined) : Promise<string[] | string | undefined>;
    /**
     * Test generating error with message property.
     * @param requestConfiguration Configuration for the request such as headers, query parameters, and middleware options.
     * @returns {RequestInformation}
     */
     toPostRequestInformation(requestConfiguration?: RequestConfiguration<object> | undefined) : RequestInformation;
}
/**
 * Serializes information the current object
 * @param writer Serialization writer to use to serialize this model
 */
// @ts-ignore
export function serializeSuccess(writer: SerializationWriter, success: Partial<string[] | string> | undefined | null = {}) : void {
    if (success === undefined || success === null) return;
    switch (true) {
        case Array.isArray(success) && (success).every(item => typeof item === 'string') :
            writer.writeCollectionOfPrimitiveValues<string>(undefined, success as string[]);
            break;
        case typeof success === "string":
            writer.writeStringValue(undefined, success as string);
            break;
    }
}
export type Success = string[] | string;
/**
 * Uri template for the request builder.
 */
export const Example1RequestBuilderUriTemplate = "{+baseurl}/example1";
/**
 * Metadata for all the requests in the request builder.
 */
export const Example1RequestBuilderRequestsMetadata: RequestsMetadata = {
    post: {
        uriTemplate: Example1RequestBuilderUriTemplate,
        responseBodyContentType: "application/json",
        adapterMethodName: "send",
        responseBodyFactory:  createSuccessFromDiscriminatorValue,
    },
};
/* tslint:enable */
/* eslint-enable */

Copy link
Member

@andrueastman andrueastman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also enable the intergration test at

"Rationale": "https://github.com/microsoft/kiota/issues/5256"

@rkodev rkodev marked this pull request as draft October 8, 2024 15:16
@rkodev rkodev force-pushed the fix/ts-composed-types branch 4 times, most recently from 7971456 to 58de04a Compare October 15, 2024 11:17
@rkodev rkodev marked this pull request as ready for review October 23, 2024 12:52
@rkodev rkodev force-pushed the fix/ts-composed-types branch 2 times, most recently from aaffa8e to 6d45335 Compare November 4, 2024 06:05
Copy link
Member

@andrueastman andrueastman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we still need to remove this line from the integration tests....

"Rationale": "https://github.com/microsoft/kiota/issues/5353"

@andrueastman
Copy link
Member

Any chance we can double check the failing intergration test?

@rkodev rkodev force-pushed the fix/ts-composed-types branch 2 times, most recently from 0daf6b8 to b77ac28 Compare November 11, 2024 15:17
Copy link

sonarcloud bot commented Nov 11, 2024

Copy link

sonarcloud bot commented Nov 11, 2024

Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for making the changes!

@rkodev rkodev merged commit 65eb7c0 into main Nov 12, 2024
214 checks passed
@rkodev rkodev deleted the fix/ts-composed-types branch November 12, 2024 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done ✔️
Development

Successfully merging this pull request may close these issues.

TypeScript - anyOf/oneOf arrays do not emit the correct serialization and deserialization information
3 participants